﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace YJKRevitWndr
{
	public partial class DlgSearch : System.Windows.Forms.Form
	{
		public string SearchContent { get; set; }

		public DlgSearch()
		{
			this.InitializeComponent();
		}

		private void DlgSearch_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			this.SearchContent = this.txtSearch.Text.Trim();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "DoorAndWindowSearch.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.txtSearch.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "DoorAndWindowSearch.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				string text = streamReader.ReadLine();
				this.txtSearch.Text = text;
			}
			catch
			{
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
		}

		private void DlgSearch_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveData();
		}
	}
}
